﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XModem.library
{
    /// <summary>
    /// 实现类似定时器功能 判断是否超时
    /// </summary>
  public  class TimerHelp
    {
        private long startTime = 0;
        private long stopTime = 0;
        private long timeout = 0;

        public TimerHelp(long timeout)
        {
            this.timeout = timeout;
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <returns></returns>
        public TimerHelp start()
        {
            this.startTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            this.stopTime = 0;
            return this;
        }
        /// <summary>
        /// 停止
        /// </summary>
        public void stop()
        {
            this.stopTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }
        /// <summary>
        /// 是否过期
        /// </summary>
        /// <returns></returns>
        public bool isExpired()
        {
            return ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds > startTime + timeout);
        }
        /// <summary>
        /// 获取开始事件
        /// </summary>
        /// <returns></returns>
        public long getStartTime()
        {
            return this.startTime;
        }
        /// <summary>
        /// 获取停止时间
        /// </summary>
        /// <returns></returns>
        public long getStopTime()
        {
            return this.stopTime;
        }
        /// <summary>
        /// 差值
        /// </summary>
        /// <returns></returns>
        public long getTotalTime()
        {
            return this.stopTime - this.startTime;
        }

        public long getTimeout()
        {
            return timeout;
        }

        public void setTimeout(long timeout)
        {
            this.timeout = timeout;
        }
        /// <summary>
        /// 是否工作
        /// </summary>
        /// <returns></returns>
        public bool isWorking()
        {
            return (stopTime != 0);
        }

    }
}
